package com.light.mapcomponents;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import com.badlogic.gdx.math.Vector2;
import com.light.buttons.Button.ButtonState;
import com.light.gamecomponents.IGameComponents;
import com.light.gamecomponents.gravitylight.GravityLight;
import com.light.gamecomponents.physics.RollerPhysicsWorld;
import com.light.roller.Player;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;
import com.light.roller.Stage;

public class PanelComponents implements IGameComponents, IActionComponents, IUpdateHandler {

	protected String[] m_texts;
	protected Roller m_game;
	protected TiledTextureRegion m_tiledTextureRegion;
	protected AnimatedSprite m_sprite;
	protected AnimatedSprite m_lightSprite;
	protected String m_resourceName = "Panel";
	protected String m_lightResourceName = "PanelLight";
	protected Vector2 m_initialPosition;
	protected TiledTextureRegion m_tiledLightTextureRegion;
	protected LoopEntityModifier m_nowModifier;
	protected float m_initialRotation;
	protected boolean m_isAnimateOn = false;
	protected float m_lightBlinkTime = 0;
	
	public PanelComponents(Roller game, 
			               Vector2 initialPosition, float initialRotation,
			               String[] texts)
	{
		m_game = game;
		m_initialPosition = initialPosition;
		m_initialRotation = initialRotation;
		m_texts = texts;
	}
	
	
	@Override
	public void action(Player player, Stage stage) {
		Roller.hintForm.showForm();
		Roller.hintForm.setRunningText(m_texts);
		
		m_game.pause();
		Roller.actionButton.setState(ButtonState.PANEL_SHOWED);
	}

	@Override
	public boolean collided(Player player) {
		if (player.getSprite().collidesWith(m_sprite))
			return true;
		else
			return false;
	}

	@Override
	public void onLoadEngine() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLoadResources() {
		ResourceManager.onLoadResources(m_resourceName, m_game);
		this.m_tiledTextureRegion = ResourceManager.getTiledTextureRegion(m_resourceName);
		ResourceManager.onLoadResources(m_lightResourceName, m_game);
		this.m_tiledLightTextureRegion = ResourceManager.getTiledTextureRegion(m_lightResourceName);
	}

	@Override
	public void unLoadResources() {
		this.m_tiledTextureRegion = null;
		ResourceManager.unLoadResources(m_resourceName, m_game);
		this.m_tiledLightTextureRegion = null;
		ResourceManager.unLoadResources(m_lightResourceName, m_game);
	}

	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWrold) {
		m_sprite = new AnimatedSprite(m_initialPosition.x,
									  m_initialPosition.y,
									  m_tiledTextureRegion);
		
		m_lightSprite = new AnimatedSprite(50, 25, m_tiledLightTextureRegion);
		
		//m_sprite.animate(50);
		scene.attachChild(m_sprite);
		scene.getLastChild().attachChild(m_lightSprite);
		m_sprite.setRotation(m_initialRotation);
		m_lightSprite.setAlpha(0.0f);
		((RollerPhysicsWorld)physicsWrold).registerUpdateHandler(this);
		
	}

	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) {
		scene.detachChild(m_sprite);
		
	}

	@Override
	public void rotate(float angle) {
		m_sprite.setRotation(angle);
		
	}
	
	public boolean isInLight(GravityLight light)
	{
		Vector2 disVec = new Vector2(getSpritePosition().x - light.getSpritePosition().x, 
				 					 getSpritePosition().y - light.getSpritePosition().y);

		float dis = (float)Math.sqrt(disVec.x * disVec.x + disVec.y * disVec.y);
		float minDis = m_sprite.getWidth() / 2 + light.getWidth() / 2 - 22;
		
		if (dis <= minDis)
			return true;
		else
			return false;
		
	}
	
	public Vector2 getSpritePosition() 
	{ 
		return new Vector2(m_sprite.getX() + m_sprite.getBaseWidth() / 2, 
				           m_sprite.getY() + m_sprite.getBaseHeight() / 2); 
	}
	
	public void startAnimate()
	{
		if (!m_isAnimateOn)
		{
			m_sprite.animate(50);
			m_isAnimateOn = true;
			//m_lightSprite.
		}
	}
	public void stopAnimate()
	{
		if (m_isAnimateOn)
		{
			m_sprite.stopAnimation();
			m_isAnimateOn = false;
		}
	}


	@Override
	public void onUpdate(float pSecondsElapsed) {
		if (m_isAnimateOn)
		{
			m_lightBlinkTime += pSecondsElapsed;
			if (m_lightBlinkTime >= 4.5f)
				m_lightBlinkTime -= 4.5f;
			
			if (m_lightBlinkTime < 2.25)
				m_lightSprite.setAlpha(1.0f * m_lightBlinkTime / 2.25f);
			else 
				m_lightSprite.setAlpha(1.0f * (2.25f - (m_lightBlinkTime - 2.25f)) / 2.25f);
		}
		
	}


	@Override
	public void reset() {
		// TODO Auto-generated method stub
		
	}

}
